// pages/build-address/build-address.js
import { addReceiveAddress, getReceiveDetails, updateReceiveAddress } from '../../api/apis.js'
const app = getApp()
Page({
  data: {
    checked: false,
    color: '#ED204F',
    addresDetail: {
      receive_name: '',
      receive_phone: '',
      region: [],
      address: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.did) {
      this.setData({
        mid: options.did
      })
      this.getAddressDetail()
    }
  },
  getAddressDetail() {
    getReceiveDetails({
      data: {
        id: this.data.mid
      },
      success: res => {
        if (res.state) {
          this.setData({
            addresDetail: {
              receive_name: res.data.receive_name,
              receive_phone: res.data.receive_phone,
              region: [res.data.province, res.data.city, res.data.area],
              address: res.data.address
            },
            checked: res.data.isDefault,
            region: res.data.province + '-' + res.data.city + '-' +res.data.area
          })
        }
      }
    })
  },
  savaData(e){
    this.data.formDetail = e.detail.value
    if (this.validateFrom()) {
      if (this.data.mid) {
        updateReceiveAddress({
          data: {
            mid: this.data.mid,
            province: e.detail.value.region[0],
            city: e.detail.value.region[1],
            area: e.detail.value.region[2],
            address: e.detail.value.address,
            receive_name: e.detail.value.receive_name,
            receive_phone: e.detail.value.receive_phone,
            isDefault: this.data.checked,
            openid: app.globalData.openid
          },
          success: res => {
            if (res.state) {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      } else {
        addReceiveAddress({
          data: {
            province: e.detail.value.region[0],
            city: e.detail.value.region[1],
            area: e.detail.value.region[2],
            address: e.detail.value.address,
            receive_name: e.detail.value.receive_name,
            receive_phone: e.detail.value.receive_phone,
            isDefault: this.data.checked,
            openid: app.globalData.openid
          },
          success: res => {
            if (res.state) {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      }
    }
  },
  chooseRegion(e) {
    this.setData({
      region: e.detail.value.join('-')
    })
  },
  setDefault() {
    this.setData({
      checked: !this.data.checked
    })
  },
  validateFrom() {
    let obj = this.data.formDetail
    if (!obj.receive_name.trim()) {
      wx.showToast({
        title: '请填写姓名',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (!obj.receive_phone.trim()) {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (obj.region.length<=0) {
      wx.showToast({
        title: '请选择地区',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (!obj.address.trim()) {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    return true
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})